package com.ruoyi.tester.executor.compile;

import java.math.BigDecimal;

/**
 * @author SongJun
 * @description 字面量(字符串, 数字, 布尔值)
 * @date 2022/4/28 18:17
 */
public class Literal {
    private Object obj;

    public Literal(String vn){
        if(vn.charAt(0) == '"' && vn.charAt(vn.length()-1) == '"'){
            obj = vn.substring(1, vn.length()-1);
        } else if(KeyWords.LITERAL_TRUE.equals(vn) || KeyWords.LITERAL_FALSE.equals(vn)){
            obj = KeyWords.LITERAL_TRUE.equals(vn);
        }
        for (int i=0; i<vn.length(); i++){
            char c = vn.charAt(i);
            if(!chartInDigitSet(c)){
                return;
            }
        }
        try {
            obj = new BigDecimal(vn);
        } catch (Exception e) {

        }
    }

    public Object getObj() {
        return obj;
    }

    private static final char[] digitSet = {'0','1','2','3','4','5','6','7','8','9','.','-'};
    private boolean chartInDigitSet(char c){
        for (char digit : digitSet) {
            if (digit == c) {
                return true;
            }
        }
        return false;
    }
}
